package com.cn.zengzeng.spbzengzeng.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.DateUtils;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 日期 工具类
 *
 * @author zhangrong
 */
@Slf4j
public class DateHelper {
    /**
     * The constant DATE_FORMAT.
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    /**
     * The constant DATE_TIME_FORMAT.
     */
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    // 世界标准时间UTC
    public static final String DATE_FORMAT_T = "yyyy-MM-dd'T'HH:mm:ss";
    // 事件服务DateTime格式
    public static final String ES_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
    // 数据服务DateTime格式
    public static final String DS_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

    public static String getNowZone() {
        LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("GMT"));
        return localDateTime.format(dtf);
    }

    /**
     * 获取0时区时间
     * @param value
     * @return
     */
    public static String getZone(String value) {
        if (!StringUtils.isEmpty(value) && !"null".equals(value)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
            df.setTimeZone(TimeZone.getTimeZone("GMT"));
            try {
                return df.format(format.parse(value));
            } catch (ParseException e) {
                log.info("错误：", e);
            }
        }
        return null;
    }
    /**
     * Parse date gracefully.
     * @param date the date
     * @return the date
     */
    public static Date parseDateGracefully(String date) {
        if (!StringUtils.hasText(date)) {
            return null;
        }
        return DateUtils.parseDate(date, new String[]{
                DATE_TIME_FORMAT,
                DATE_FORMAT,
        });
    }

    public static String formatZonedDateTime(Date date,String dateFormat) {
        if(StringUtils.isEmpty(dateFormat)){
            dateFormat = DATE_FORMAT_T;
        }
        SimpleDateFormat df = new SimpleDateFormat(dateFormat);
        return df.format(date);
    }

    public static Date localToUTC(Date date){
        Calendar calendar= Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        /** 取得时间偏移量 */
        int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
        /** 取得夏令时差 */
        int dstOffset = calendar.get(Calendar.DST_OFFSET);
        /** 从本地时间里扣除这些差量，即可以取得UTC时间*/
        calendar.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        /** 取得的时间就是UTC标准时间 */
        return new Date(calendar.getTimeInMillis());
    }
    /**
     * 0时区转换8时区
     * @return
     */
    public static String getThreeDaysAndEightHoursAgo(String time1) throws ParseException {
        String a = time1;
        a = a.replace("T", " ").substring(0, a.indexOf("."));
        System.out.println(a);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:s");
        Date date = dateFormat.parse(a); // 指定日期
        long time = date.getTime(); // 得到指定日期的毫秒数
        long days = (8) * 60 * 60 * 1000L; // 要加上的天数转换成毫秒数
        time += days; // 相加得到新的毫秒数
        Date newDate = new Date(time);
        String format = dateFormat.format(newDate);
        return format;
    }

    public static String dateToTdate(String dateTime) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = df.parse(dateTime);
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        String format = df1.format(parse);
        return format ;
    }

    /**
     * @Auther zhangzengzeng
     * @Description  日期转时间戳
     * @Date 2021/5/13 0013 10:59
     * @param dateString:
     * @return java.lang.Long
     **/
    public static Long dateToTime(String dateString)  {
        long time = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            time = sdf.parse(dateString).getTime();
        }catch(ParseException e) {
            log.error(e.getMessage());
        }
        return time ;
    }

    //获取前一天凌晨时间
    public static Date getYesterdayStartTime(){
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 0);
        todayEnd.set(Calendar.MINUTE, 0);
        todayEnd.set(Calendar.SECOND, 0);
        todayEnd.add(Calendar.DATE, -1);
        //todayEnd.set(Calendar.MILLISECOND, 0);
        return todayEnd.getTime();
    }
    //获取昨天半夜23:59:59
    public static Date getYesterdayEnd(){
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        //todayEnd.set(Calendar.MILLISECOND, 999);
        todayEnd.add(Calendar.DATE, -1);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        return todayEnd.getTime();
    }

    public static String dateFormat(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
    /**
     * @Auther zhangzengzeng
     * @Description  默认生日
     * @Date 2021/9/13 10:51
     * @return java.lang.String
     **/
    public static String toBirthday(){
        Date date1 = strToDate("1970-01-01");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date1);
    }
    /**
     * @Auther zhangzengzeng
     * @Description  默认时间
     * @Date 2021/9/13 15:52
     * @return java.lang.String
     **/
    public static String defaultDate(){
        Date date1 = todate("1970-01-01 00:00:00");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date1);
    }
    public static Date strToDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = sdf.parse(str);
            return parse ;
        }catch(ParseException e) {
            e.printStackTrace();
        }
        return parse ;
    }
    public static Date todate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = sdf.parse(str);
            return parse ;
        }catch(ParseException e) {
            e.printStackTrace();
        }
        return parse ;

    }
    /**
     * @Auther zhangzengzeng
     * @Description  时间戳转日期 /毫秒
     * @Date 2021/8/30 10:38
     * @param milliseconds:
     * @return
     **/
    public static Date longToDate (long milliseconds){
        return new Date(milliseconds);
    }
    /**
     * @Auther zhangzengzeng
     * @Description  加月份
     * @Date 2021/8/30 13:53
     * @param date:
     * @return void
     **/
    public static Date addMonth(Date date,int amount){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,amount);
        return calendar.getTime();
    }
    /**
     * @Auther zhangzengzeng
     * @Description  加小时
     * @Date 2021/8/30 13:53
     * @param date:
     * @return void
     **/
    public static Date addHours(Date date,int amount){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR,amount);
        return calendar.getTime();
    }

    /**
     * @Auther zhangzengzeng
     * @Description  加分钟
     * @Date 2021/8/30 13:53
     * @param date:
     * @return void
     **/
    public static Date addMinutes(Date date,int amount){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE,amount);
        return calendar.getTime();
    }
    /**
     * @Auther zhangzengzeng
     * @Description  日期转时间戳/秒
     * @Date 2021/9/1 10:48
     * @param str:
     * @return void
     **/
    public static Long strToSecond(String str){
        long time = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            time = sdf.parse(str).getTime()/1000;
        }catch(ParseException e) {
            log.error(e.getMessage());
        }
        return time ;
    }
    /**
     * @Auther zhangzengzeng
     * @Description  秒转成时间
     * @Date 2021/9/1 10:57
     * @param second:
     * @return void
     **/
    public static String secondToString(Long second){
        String s = null;
        try {
            Date date = new Date(second * 1000);
            s = dateFormat(date);
        }catch(Exception e) {
            log.error(e.getMessage());
        }
        return s ;
    }
    public static void main (String[] args) {
        Long aLong = strToSecond("2021-09-01 10:40:37");
        System.out.println("aLong = " + aLong);
    }

    /*
     * @Auther zhangzengzeng
     * @Description
     * @Date 2021/9/3 9:56
     * @param null:
     * @return
     **/
    public static Long dateToSecond(Date date){
        String s = dateFormat(date);
        Long aLong = strToSecond(s);
        return aLong ;
    }
    /**
     * @Auther zhangzengzeng
     * @Description  字符串转毫秒
     * @Date 2021/9/3 10:09
     * @param str:
     * @return java.lang.Long
     **/
    public static Long stringToMilliseconds(String str){
        Date date = todate(str);
        return date.getTime();
    }
    /**
     * @Auther zhangzengzeng
     * @Description  毫秒转时间字符串
     * @Date 2021/9/2 12:48
     * @return
     **/
    public static String longToString(Long milliseconds){
        String s = null ;
        try {

            Date date = new Date(milliseconds);
            s = dateFormat(date);
        }catch(Exception e) {
            e.printStackTrace();
        }
        return s;
    }
}
